<?php 
	class Message extends CI_Controller{
		public function __construct(){
			parent::__construct();
		}
		
		
			public function get_message($imie="",$customer_name=""){

			///รับค่าเกบไว้ที่ตัวแปร 
			$num=0;
			//$imie = "358867040228009";  //paramitor
			//$customer_name = "sherman";
			//===
			$this->db->where("name",$customer_name);
			$row_customer = $this->db->get("mobile_customer")->row();
			// select * from mobile_customer where name="praramitor $c_name";
			//===
			if($imie != ""){
				$this->db->where("imei",$imie);
				$this->db->where("customer_pkid",$row_customer->pkid);
				$num = $this->db->count_all_results("mobile_member");
		
			}
			if($num == 0){
				$tmp['pkid']			="";
				$tmp['title']			="";
				$tmp['detail']			="";
				$tmp['sentdate']			="";
				$tmp['sentby']			="";
				$arrs[] 		=$tmp;
				echo json_encode($arrs);
			}else{
				$this->db->where("imei",$imie);
				$this->db->where("customer_pkid",$row_customer->pkid);
				$row_mem = $this->db->get("mobile_member")->row();
				/////////////////////////////////////////
				$this->db->select("t2.pkid");
				$this->db->from("mobile_message t1");
				$this->db->join("mobile_mes_natificate t2","t1.pkid = t2.message_pkid");
				$this->db->where("t2.status",0);
				$this->db->where("t2.member_pkid",$row_mem->pkid);
				$rowm = $this->db->get()->row();
				//print_r($row_mem);
				//print_r($rowm);
					if($rowm){
						$this->acc_message($rowm->pkid,$row_mem->pkid);
					}else{
						$tmp['pkid']			="";
						$tmp['title']			="";
						$tmp['detail']			="";
						$tmp['sentdate']			="";
						$tmp['sentby']			="";
						$arrs[] 		=$tmp;
						echo json_encode($arrs);
						
						}
	
			}
			
		}
		
		public function acc_message($id,$mid){
				
				//echo $id;
			//	echo $mid;
				
				$this->db->select("t1.pkid,t1.title,t1.detail,t1.sentdate,t1.sentby");
				$this->db->from("mobile_message t1");
				$this->db->join("mobile_mes_natificate t2","t1.pkid = t2.message_pkid");
				$this->db->order_by("t2.pkid","desc");
				$this->db->where("t2.status",0);
				$this->db->where("t2.member_pkid",$mid);
				$this->db->where("t2.pkid",$id);
		
				
				$res = $this->db->get()->row();
				
		
				$tmp['pkid']			=$res->pkid;
				$tmp['title']			=$res->title;
				$tmp['detail']			=$res->detail;
				$tmp['sentdate']		=$res->sentdate;
				$tmp['sentby']		=$res->sentby;
				$arrs[] 		=$tmp;
				
			
				
				$this->db->where("pkid",$id);
				$this->db->where("member_pkid",$mid);
				$this->db->update("mobile_mes_natificate",array("status"=>1));		
			echo json_encode($arrs);
		}	
		
		
		public function message_list($str="",$imie=""){
			$arr = array();
			if($str !="" && $imie != ""){
				$this->db->select("t2.title,t2.detail");
				$this->db->from("mobile_mes_natificate t1");
				$this->db->join("mobile_message t2", "t1.message_pkid=t2.pkid","left");
				$this->db->join("mobile_customer t3", "t1.customer_pkid=t3.pkid","left");
				$this->db->join("mobile_member t4", "t1.member_pkid=t4.pkid","left");
				$this->db->where("t3.name",$str);
				$this->db->where("imei",$imie);
				$this->db->order_by("t1.pkid","desc");
				$res = $this->db->get()->result_array();
				foreach ($res as $index){
					$tmp['title']		=$index['title'];
					$tmp['detail']		=$index['detail'];
					$arr[] =$tmp;
				}
			}
			echo json_encode($arr);
		}
	
	}
?>